<template>
    <view>
        <my-confirm v-model="confirmVisible" :msg="confirmMsg" @confirm="confirmCallback" :title="confirmTitle"
                    :confirm-input="confirmInput" :show-cancel="confirmCancelBt"
                    @cancel="confirmCancel"></my-confirm>
        <my-toptip ref="toptip"></my-toptip>
    </view>
</template>
<script>

    export default {
        data() {
            return {
                confirmVisible: false,
                confirmMsg: '',
                confirmTitle: '操作确认',
                confirmInput: false,
                confirmCancelBt: true,
                confirmCallback() {

                },
                confirmCancel() {

                },
                layerVisible: false         //解决小程序textarea穿透问题
            }
        },
        methods: {

            $alert(msg, title = '出错啦') {
                this.confirmCancelBt = false;
                this.layerVisible = true;
                return this.$confirm(msg, () => {
                    this.confirmCancelBt = true;
                }, title);
            },
            $notice(msg, title = '提示') {
                this.$alert(msg, title);
            },
            $confirm(msg, callback, title = '操作确认') {
                const that = this;
                this.confirmTitle = title;
                this.confirmMsg = msg;
                this.confirmInput = false;

                switch (typeof callback) {
                    case "function":
                        this.confirmCallback = function () {
                            that.layerVisible = false;
                            callback();
                        };
                        break;
                    case "string":
                        this.confirmTitle = callback;
                        return new Promise((resolve, reject) => {
                            this.confirmCallback = function () {
                                that.layerVisible = false;
                                resolve();
                            };
                            this.confirmCancel = function () {
                                that.layerVisible = false;
                                reject();
                            }
                        });
                    default:
                        return new Promise((resolve, reject) => {
                            this.confirmCallback = function () {
                                that.layerVisible = false;
                                resolve();
                            };
                            this.confirmCancel = function () {
                                that.layerVisible = false;
                                reject();
                            }
                        });
                }
                this.confirmVisible = true;
                that.layerVisible = true;
            },
            $toptip(msg, closeTime = 2000, type = 'danger') {
                this.$refs.toptip.show(msg, type, closeTime);
            },
            $warning(msg, closeTime) {
                this.$refs.toptip.show(msg, 'warning', closeTime);
            },

            $prompt(title, callback) {
                const that = this;
                this.confirmTitle = title;
                this.confirmInput = true;
                this.confirmVisible = true;

                if (typeof callback === 'function') {
                    this.confirmCallback = function (promptInput) {
                        that.layerVisible = false;
                        callback(promptInput);
                    }
                } else {
                    return new Promise((resolve) => {
                        this.confirmCallback = function (promptInput) {
                            that.layerVisible = false;
                            resolve(promptInput);
                        }
                    });
                }
            },
            $back(delta = 1) {
                uni.navigateBack({
                    delta
                });
            },
            $toHome() {
                uni.reLaunch({
                    url: '/pages/welcome'
                });
            }
        }
    }
</script>

<style scoped>

</style>
